<template>
  <div class="container">
    <div class="login">
          <div class="tx">
                <img src="@/assets/logo.png" alt="">
            </div>
    <el-form :model="user" ref="loginForm" :rules="rules">
      <el-form-item prop="username">
        <el-input
          placeholder="请输入用户名"
          v-model="user.username"
          prefix-icon="el-icon-user-solid"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%" @click="doLogin" 
          >立即登陆</el-button
        >
      </el-form-item>
    </el-form>
    </div>

  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      //用户信息
      user: {
        username: "admin",
        password: "123456",
      },
      //校验的字段名，用户名密码
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {},
   methods: {
    //执行登录的操作
    doLogin() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          let url = this.$store.state.baseUrl; //请求接口的基地址
          //请求登录的接口地址
          this.$axios.post(`${url}login`, this.user).then((res) => {
            console.log(res);
            //判断用户登录成功后要做什么
            if (res.meta.status == 200) {
              //登陆成功
              //1.获取用户登录的token值,并且存储到vuex中
              this.$store.commit("login", res.data);
              //2.跳转到管理后台首页
              this.$router.push("/admin/home");
              //登录成功后的提示信息
              this.$message.success("登陆成功");
            } else {
              //登陆失败的信息
              this.$message.error(res.meta.msg);
            }
          });
        } else {
          //失败时的校验
          console.log("error submit!!");
          return false;
        }
      });
    },
  
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss">
.container{
  background-color: #2b4b6b;
  height: 100%;
}
.login{
  width: 400px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.tx{
  height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
}
.el-form{
   position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>